package com.shockweb.common.calculate.parse;

import java.util.List;
import java.util.ArrayList;

import com.shockweb.common.calculate.api.IFunction;
import com.shockweb.common.calculate.util.SimpleCalculate;

/**
 * 节点转换树类
 * @author pengminghua
 */
public class ExpressionNode {
  

	private String key = null;
	
	/**
	 * 构造子节点
	 * @param key
	 */
	public ExpressionNode(String key){
		this.key = key;
	}
	/**
	 * 返回key
	 * @return String
	 */
	public String getKey(){
		return key;
	}
	
	private SimpleCalculate simpleCalculate = null; 
	
	/**
	 * 返回计算器
	 * @return SimpleCalculate
	 */
	public SimpleCalculate getSimpleCalculate(){
		return simpleCalculate;
	}
	
	/**
	 * 设置计算器
	 * @param simpleCalculate
	 */
	public void setSimpleCalculate(SimpleCalculate simpleCalculate){
		this.simpleCalculate = simpleCalculate;
	}
	
	private IFunction function = null; 
	
	/**
	 * 返回计算的函数
	 * @return IFunction
	 */
	public IFunction getFunction(){
		return function;
	}
	
	/**
	 * 设置计算的函数
	 * @param function
	 */
	public void setFunction(IFunction function){
		this.function = function;
	}
	
	
	private StringBuffer expression = null;
	
	/**
	 * 设置计算表达式
	 * @param expression
	 */
	public void setExpression(StringBuffer expression){
		this.expression = expression;
	}
	
	/**
	 * 返回计算表达式
	 * @return StringBuffer
	 */
	public StringBuffer getExpression(){
    	return expression;
	}

  	private List<ExpressionNode> nodes = null;
  	
  	/**
  	 * 返回所有子节点 
  	 * @return List
  	 */
  	public List<ExpressionNode> getNodes(){
    	return nodes;
  	}
  	
  	/**
  	 * 增加一个子节点
  	 * @param node
  	 */
  	public void addNode(ExpressionNode node){
  	  	if(nodes == null){
      		nodes = new ArrayList<ExpressionNode>();
    	}
    	nodes.add(node);
  	}
  	
  	/**
  	 * 返回子节点的数量
  	 * @return int
  	 */
  	public int getNodeSize(){
    	if(nodes != null){
      		return nodes.size();
    	}else{
      		return 0;
    	}
  	}
  	
	private Object value = null;
	
	/**
	 * 获取表达式预算的值
	 * @return Object
	 */
	public Object getValue(){
		return value;
	}
	
	/**
	 * 缓存表达式预算的值
	 * @param value
	 */
	public void setValue(Object value){
		this.value = value;
	}
	
  	/**
  	 * 获取第index的子节点
  	 * @param index
  	 * @return ExpressionNode
  	 */
  	public ExpressionNode getNode(int index){
    	return (ExpressionNode)nodes.get(index);
  	}

  	public String toString(){
  		return toString("");
  	}
  	
  	/**
  	 * @see Object#toString()
  	 * @param blank
  	 * @return String
  	 */
	public String toString(String blank){
		StringBuffer sb = new StringBuffer(blank + "key: " + key + "\n");
		sb.append(blank + "expression: " + expression + "\n");
		sb.append(blank + "value: " + value + "\n");
		if(simpleCalculate!=null){
			sb.append(blank + "calculator: ").append(simpleCalculate);
		}
		if(function!=null){
			sb.append(blank + "calculator: ").append(function);
		}
		if(nodes!=null){
			sb.append("\n" + blank + "{");
			for(int i=0;i<nodes.size();i++){
				sb.append("\n").append(((ExpressionNode)nodes.get(i)).toString(blank + "    "));
			}
			sb.append("\n" + blank + "}");
		}
		return sb.append(";").toString();
	}
}

 